--Damage Facilitation Functions. Called early in the damage process to determine the exact process for damaging a particular type of entity. These functions are seperated from the core file so they can be easily added to.

CDamage = {}
CDamage["prop"] = {}
CDamage["wreckage"] = {}
CDamage["organic"] = {}
CDamage["shield"] = {}

CDamage["prop"]["normal"] = function(entity, damage, player) CDamage_Damage_Prop_Normal(entity, damage, player) end
CDamage["prop"]["anti-shield"] = function(entity, damage, player) CDamage_Damage_Prop_AntiShield(entity, damage, player) end
CDamage["prop"]["destructive"] = function(entity, damage, player) CDamage_Damage_Prop_Destructive(entity, damage, player) end
CDamage["wreckage"]["normal"] = function(entity, damage, player) CDamage_Damage_Wreckage(entity, damage, player) end
CDamage["wreckage"]["destructive"] = function(entity, damage, player) CDamage_Damage_Wreckage(entity, damage, player) end
CDamage["organic"]["normal"] = function(entity, damage, player) CDamage_Damage_Organic(entity, damage, player) end
CDamage["organic"]["destructive"] = function(entity, damage, player) CDamage_Damage_Organic(entity, damage, player) end
CDamage["shield"]["normal"] = function(entity, damage, player) CDamage_Damage_Shield(entity, damage, player) end
CDamage["shield"]["anti-shield"] = function(entity, damage, player) CDamage_Damage_Shield(entity, damage, player) end

function CDamage_Damage_Shield(entity, damage, player)
	entity:ReduceIntegrity(damage)
end

function CDamage_Damage_Wreckage(entity, damage, player)
	if (damage * 2 >= entity.CDamage.maxhealth) then
		entity.fading = true
	end
end

function CDamage_Damage_Prop_AntiShield(entity, damage, player)
	if (entity.CDamage.isshielded) then
		if (entity.CDamage.shield:IsValid()) then
			entity.CDamage.shield:DamageShield(damage)
			DoPropSpawnedEffect(entity)
		else
			entity.CDamage.isshielded = false
			entity:SetNetworkedBool("IsShielded", false)
		end
	end
end

function CDamage_Damage_Prop_Normal(entity, damage, player)
	if (entity.CDamage.isshielded == true) then
		if (entity.CDamage.shield:IsValid()) then
			DoPropSpawnedEffect(entity)
			if (entity.CDamage.shield.HP < damage) then
				damage = damage - entity.CDamage.shield.HP
				entity.CDamage.shield:DamageShield(entity.CDamage.shield.HP)
			else
				entity.CDamage.shield:DamageShield(damage)
				damage = 0
			end
		else
			entity.CDamage.isshielded = false
			entity:SetNetworkedBool("IsShielded", false)
		end
	end
	if damage > 0 then
		if (util.tobool(GetConVarNumber("sv_CDamage_contraptiondamage"))) then
			local contraption = CDE_FindContraption( entity )
			CDamage_damagecontraption(contraption, damage, player)
		else
			if (entity.CDamage.health < damage) then
				if (util.tobool(GetConVarNumber("sv_CDamage_wreckage"))) then
					CDamage_wreckentity(entity, player)
				else
					CDamage_destroyentity(entity, player)
				end
			else
				entity.CDamage.health = entity.CDamage.health - damage
			end
		end
	end
end

function CDamage_Damage_Prop_Destructive(entity, damage, player)
	if (entity.CDamage.isshielded == true) then
		DoPropSpawnedEffect(entity)
		return 0
	elseif (util.tobool(GetConVarNumber("sv_CDamage_contraptiondamage"))) then
		local contraption = CDE_FindContraption( entity )
		CDamage_damagecontraption(contraption, damage, player)
	else
		if (entity.CDamage.health < damage) then
			if (util.tobool(GetConVarNumber("sv_CDamage_wreckage"))) then
				CDamage_wreckentity(entity, player)
			else
				CDamage_destroyentity(entity, player)
			end
		else
			entity.CDamage.health = entity.CDamage.health - damage
		end
	end
end

function CDamage_Damage_Organic(entity, damage, player)
	entity:TakeDamage(damage, player, nil)
end